package com.zfzs.post.rest;

import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.modules.dto.LogQueryCriteria;
import com.zfzs.post.service.LogService;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 日志
 *
 * @author ck
 * @date 2018-11-24
 */
@RestController
@RequestMapping("api")
public class LogController {

    @Autowired
    private LogService logService;

    @GetMapping(value = "/logs")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseResult getLogs(LogQueryCriteria criteria, Pageable pageable) {
        criteria.setLogType("INFO");
        return ResponseResult.ok(logService.queryAll(criteria, pageable));
    }

    @GetMapping(value = "/logs/user")
    public ResponseResult getUserLogs(LogQueryCriteria criteria, Pageable pageable) {
        criteria.setLogType("INFO");
        criteria.setUsername(SecurityUtils.getUsername());
        return ResponseResult.ok(logService.queryAllByUser(criteria, pageable));
    }

    @GetMapping(value = "/logs/error")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseResult getErrorLogs(LogQueryCriteria criteria, Pageable pageable) {
        criteria.setLogType("ERROR");
        return ResponseResult.ok(logService.queryAll(criteria, pageable));
    }

    @GetMapping(value = "/logs/error/{id}")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResponseResult getErrorLogs(@PathVariable Long id) {
        return ResponseResult.ok(logService.findByErrDetail(id));
    }
}
